//
//  QRcodeViewController.m
//  BaChat
//
//  Created by dllo on 16/6/16.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "QRcodeViewController.h"
#import <CoreImage/CoreImage.h>
@interface QRcodeViewController ()

@property (nonatomic, retain)UIImageView *HeadImageView;
@property (nonatomic, retain)UILabel *UserLabel;
@property (nonatomic, retain)UILabel *RegionLabel;
@property (nonatomic, retain)UIImageView *GenderImageView;


@end

@implementation QRcodeViewController
-(void)dealloc
{
    [_HeadImageView release];
    [_UserLabel release];
    [_RegionLabel release];
    [_GenderImageView release];
    [super dealloc];
}

#pragma mark -- 使用给定的字符创获得CIImage类型的对象
-(CIImage *)getImageByString:(NSString *)dataString
{
    //首先判断字符串是否合理!
    if (!dataString || dataString == nil || [dataString isEqualToString:@""]) {
        return nil;
    }
    
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //1. 设置filter的默认值,防止之前的设置对本次转化有影响
    [filter setDefaults];
    //2. 将传入的字符串转换为NSData
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    //3. 将NSData传递给滤镜(通过KVC的方式,设置inputMessage)
    [filter setValue:data forKey:@"inputMessage"];
    //4. 由filter对象输出图像
    CIImage *outputImage = [filter outputImage];
    
    //5. 返回二维码图像
    return outputImage;
}

#pragma mark -- 对图像进行清晰处理，很关键！
- (UIImage *)excludeFuzzyImageFromCIImage: (CIImage *)image size: (CGFloat)size

{
    
    CGRect extent = CGRectIntegral(image.extent);
    
    //通过比例计算，让最终的图像大小合理（正方形是我们想要的）
    CGFloat scale = MIN(size / CGRectGetWidth(extent), size / CGRectGetHeight(extent));
    
    size_t width = CGRectGetWidth(extent) * scale;
    
    size_t height = CGRectGetHeight(extent) * scale;
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpace, (CGBitmapInfo)kCGImageAlphaNone);
    
    CIContext * context = [CIContext contextWithOptions: nil];
    
    CGImageRef bitmapImage = [context createCGImage: image fromRect: extent];
    
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    
    CGContextScaleCTM(bitmapRef, scale, scale);
    
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    
    //切记ARC模式下是不会对CoreFoundation框架的对象进行自动释放的，所以要我们手动释放
    CGContextRelease(bitmapRef);
    
    CGImageRelease(bitmapImage);
    
    CGColorSpaceRelease(colorSpace);
    
    return [UIImage imageWithCGImage: scaledImage];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];

    
   
    // 用户头像
    self.HeadImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"head"]];
    self.HeadImageView.frame = CGRectMake( 10, 10, 70, 70);
    self.HeadImageView.backgroundColor = [UIColor grayColor];
    self.HeadImageView.layer.masksToBounds = YES;
    self.HeadImageView.layer.cornerRadius = 5;
    self.HeadImageView.layer.borderWidth = 1;
    [self.view addSubview:self.HeadImageView];
    [_HeadImageView release];
    // 用户名字
    self.UserLabel = [[UILabel alloc] init];
    self.UserLabel.backgroundColor = [UIColor clearColor];
    self.UserLabel.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    self.UserLabel.frame = CGRectMake(90, 15, 100, 35);
    self.UserLabel.textColor = [UIColor blackColor];
    self.UserLabel.font =[UIFont systemFontOfSize:21];
    [self.view addSubview:self.UserLabel];
    [self.UserLabel release];
    
/*
    // 用户地区
    self.RegionLabel = [[UILabel alloc] init];
    self.RegionLabel.backgroundColor = [UIColor clearColor];
    self.RegionLabel.text = @"等待传值";
    self.RegionLabel.frame = CGRectMake(90, 50, 150, 35);
    self.RegionLabel.textColor = [UIColor grayColor];
    self.RegionLabel.font =[UIFont systemFontOfSize:13];
    [self.view addSubview:self.RegionLabel];
    [self.RegionLabel release];
    // 用户性别图片
    self.GenderImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"head"]];
    self.GenderImageView.frame = CGRectMake( 190, 20, 25, 25);
    self.GenderImageView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.GenderImageView];
    [_GenderImageView release];
    
 */
    
    //取消
    UIImageView *cancelImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"popover_icon_close"]];
    cancelImage.frame = CGRectMake(220, 10, 30, 30);
    [self.view addSubview:cancelImage];
    [cancelImage release];
    
    
    
    // 二维码
    UIImageView *imageView = [[UIImageView alloc] init];
    
//    imageView.center = self.view.center;
        [self.view addSubview:imageView];

    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.view.mas_centerY).offset(10);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(self.view).multipliedBy(0.9);
        make.height.mas_equalTo(self.view.mas_width).multipliedBy(0.9);
    }];
    imageView.userInteractionEnabled = YES;
    
    
    
    NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    
    CIImage *ciImage = [self getImageByString:userName];
    
    imageView.image = [self excludeFuzzyImageFromCIImage:ciImage size:200];
    [imageView release];
    
    
    // 最下面的一行文字
    UILabel *label = [[UILabel alloc] init];
   // label.backgroundColor = [UIColor whiteColor];
    label.text = @"扫一扫上面的二维码图案,加我好友";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor grayColor];
    label.font =[UIFont systemFontOfSize:15];
    [self.view addSubview:label];

    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(imageView.mas_bottom).offset(10);
        make.left.mas_equalTo(imageView);
        make.width.mas_equalTo(imageView);
        make.height.mas_equalTo(30);
    }];
      [label release];
    
    
//    // 二维码中间的白色底层小View
//    UIView *view = [[UIView alloc] init];
//    view.frame = CGRectMake(0, 0, 50, 50);
//    view.backgroundColor = [UIColor whiteColor];
//    view.layer.masksToBounds = YES;
//    view.layer.cornerRadius = 5;
//    [imageView addSubview:view];
//[view mas_makeConstraints:^(MASConstraintMaker *make) {
//    make.center.mas_equalTo(imageView);
//    
//}];
//        [view release];
//    // 二维码中间的用户头像
//    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
//    image.frame = CGRectMake( 15, 15, 40, 40);
//    image.layer.masksToBounds = YES;
//    image.layer.cornerRadius = 5;
//    image.layer.borderWidth = 1;
//    image.center = self.view.center;
//    image.backgroundColor = [UIColor grayColor];
//    [view addSubview:image];
//    
////    [image mas_makeConstraints:^(MASConstraintMaker *make) {
////        
////        make.center.mas_equalTo(view);
////    }];
//    [image release];
//
    
}






-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(500);
        
    }];
}
@end
